package engine.graphics.popup;

import util.ArrayList;
import util.List;
import engine.CityAction;
import engine.GameEngine;
import engine.Midlet;
import engine.UnitAction;
import engine.data.City;
import engine.data.Unit;
import engine.graphics.MapCanvas;
import engine.graphics.MapCanvasEngine;
import engine.step.BeginTurnProcessStep;
import engine.step.CheckCitiesStep;
import engine.step.CheckTechnologyStep;
import engine.step.Step;

public final class PopupManager {

	public static boolean popupDisplayed;

	public static Popup popup;

	public static List popupTypes = new ArrayList();

	private PopupManager() {
		// no constructor
	}

	public static void hidePopup() {
		MapCanvasEngine.canvas.displayMap();
		if (popup.getClass().equals(PopupTerrain.class)) {
			PopupTerrain popupTerrain = (PopupTerrain) popup;
			if (popupTerrain.selectedObject != null) {
				if (popupTerrain.selectedObject.getClass().equals(City.class)) {
					showPopupCity((City) popupTerrain.selectedObject);
					return;
				} else if (popupTerrain.selectedObject.getClass().equals(Unit.class))
					Midlet.canvasEngine.setPlayerCurrUnit((Unit) popupTerrain.selectedObject);
			}
		} else if (popup.getClass().equals(PopupCity.class)) {
			if (Step.lastTurnAction.getClass().equals(CheckCitiesStep.class)) {
				Step.nextTurnAction(CheckCitiesStep.getStep());
				popupDisplayed = false;
				return;
			}
		} else if (popup.getClass().equals(PopupOK.class)) {
			byte popupType = ((Byte) popupTypes.get(0)).byteValue();
			PopupOK popupOK = (PopupOK) popup;
			if (popupType == PopupOK.POPUP_CITY) {
				showPopupCity((City) popupOK.object);
				return;
			} else if (popupType == PopupOK.POPUP_TECHNOLOGY) {
				showPopupTechnology();
				return;
			}
			popupTypes.remove(0);
		} else if (popup.getClass().equals(PopupTechnology.class)) {
			PopupTechnology popupTechnology = (PopupTechnology) popup;
			GameEngine.currPlayer.technology = popupTechnology.selectedTechnology;
		} else if (popup.getClass().equals(PopupUnitOrders.class)) {
			PopupUnitOrders popupUnitOrders = (PopupUnitOrders) popup;
			if (popupUnitOrders.selectionMode)
				Midlet.canvasEngine.showCursor(popupUnitOrders.selectionX, popupUnitOrders.selectionY);
			else if (popupUnitOrders.command != UnitAction.NONE) {
				Midlet.canvasEngine.doAction(popupUnitOrders.command);
				if (popupUnitOrders.command == UnitAction.BUILD)
					return;
			}
		} else if (popup.getClass().equals(PopupProduction.class)) {
			PopupProduction popupProduction = ((PopupProduction) popup);
			GameEngine.doCityAction(new CityAction(popupProduction.city, popupProduction.selectedType,
					popupProduction.selectedId));
			showPopupCity(popupProduction.city);
			return;
		} else if (popup.getClass().equals(PopupUnits.class)) {
			byte popupType = ((Byte) popupTypes.get(0)).byteValue();
			popupTypes.remove(0);
			PopupUnits popupUnits = ((PopupUnits) popup);
			if (popupType == PopupUnits.POPUP_FORTIFIED_UNITS) {
				GameEngine.unfortifyCityUnits(popupUnits.getCity(), popupUnits.selectedObjects);
				showPopupCity(popupUnits.getCity());
				return;
			} else if (popupType == PopupUnits.POPUP_ATTACHED_UNITS) {
				if (popupUnits.selectedObject != null) {
					GameEngine.setCurrUnit((Unit) popupUnits.selectedObject);
					Midlet.canvasEngine.autocenter();
				} else {
					showPopupCity(popupUnits.getCity());
					return;
				}
			}
		} else if (popup.getClass().equals(PopupBuildings.class)) {
			PopupBuildings popupBuildings = ((PopupBuildings) popup);
			showPopupCity(popupBuildings.city);
			return;
		} else if (popup.getClass().equals(PopupMap.class)) {
			PopupMap popupMap = ((PopupMap) popup);
			Midlet.canvasEngine.showCursor(popupMap.selectX, popupMap.selectY);
			return;
		}
		popupDisplayed = false;
		if (Step.lastTurnAction.getClass().equals(CheckTechnologyStep.class)) {
			Step.nextTurnAction(BeginTurnProcessStep.getStep());
			return;
		}
		Midlet.canvasEngine.autocenter();
	}

	public static void showPopupUnitOrders() {
		popup = new PopupUnitOrders(GameEngine.currPlayer.currUnit, GameEngine.getPossibleActions());
		popupShow();
	}

	public static void showPopupTerrain(final Unit cursor) {
		popup = new PopupTerrain(GameEngine.currPlayer, GameEngine.map.getTile(cursor.x, cursor.y), cursor.x, cursor.y);
		popupShow();
	}

	public static void showPopupOK(final byte popupType, final String text, final Object object) {
		popup = new PopupOK(text, object);
		popupTypes.add(0, new Byte(popupType));
		popupShow();
	}

	public static void showPopupCity(final City city) {
		popup = new PopupCity(GameEngine.currPlayer, city);
		popupShow();
	}

	public static void showPopupCity(final City city, final byte initPanel) {
		popup = new PopupCity(GameEngine.currPlayer, city, initPanel);
		popupShow();
	}

	public static void showPopupTechnology() {
		popup = new PopupTechnology(GameEngine.currPlayer, GameEngine.getPossibleTechnologies());
		popupShow();
	}

	public static void showPopupBuildings(final City city) {
		popup = new PopupBuildings(city, city.buildingRoster);
		popupShow();
	}

	public static void showPopupProduction(final City city) {
		popup = new PopupProduction(city, GameEngine.getPossibleBuildings(city), GameEngine.getPossibleUnits());
		popupTypes.add(0, new Byte((byte) 0));
		popupShow();
	}

	public static void showPopupFortifiedUnits(final City city) {
		popup = new PopupUnits(city, city.units, true);
		popupTypes.add(0, new Byte(PopupUnits.POPUP_FORTIFIED_UNITS));
		popupShow();
	}

	public static void showPopupAttachedUnits(final City city) {
		popup = new PopupUnits(city, city.unitsRoster, false);
		popupTypes.add(0, new Byte(PopupUnits.POPUP_ATTACHED_UNITS));
		popupShow();
	}

	public static void showPopupMap() {
		popupDisplayed = true;
		popup = new PopupMap(GameEngine.currPlayer, GameEngine.map);
		popup.show(MapCanvas.SCREEN_WIDTH - popup.width, MapCanvas.SCREEN_HEIGHT - popup.height);
	}

	private static void popupShow() {
		popupDisplayed = true;
		popup.show((MapCanvas.SCREEN_WIDTH - popup.width) / 2, (MapCanvas.SCREEN_HEIGHT - popup.height) / 2);
	}

}
